<template>
    <div class="goodinfo">
         <!-- 小球显示和隐藏 -->
            <transition 
              @before-enter="beforeEnter"
              @enter="enter"
              @after-enter="afterEnter">
                <div class="ball" v-show="ballFlag" ref="ball"></div>
            </transition>

         <!-- 商品轮播图区域 -->
            <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                <!-- 御剑乘风来 除魔天地间 -->
                     <swiper :list="list" :isfull="false"></swiper>
                </div>
            </div>
            </div>

         <!-- 商品购买区域 -->
            <div class="mui-card">
                <div class="mui-card-header">{{goodsInfo.title}}</div>
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <p class="price">
                          市场价：<del>￥{{goodsInfo.market_price}}</del>&nbsp;&nbsp;销售价：<span class="now_price">￥{{goodsInfo.sell_price}}</span>
                        </p>
                        <p>购买数量：<numbox @getcount="getSelectedCount" :max="goodsInfo.stock_quantity"></numbox></p>
                        <p>
                          <mt-button type="primary" size="small">立即购买</mt-button>
                          <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
                        </p>
                    </div>
                </div>
            </div>
            <!-- 商品参数区域 -->
            <div class="mui-card">
              <div class="mui-card-header">商品参数</div>
              <div class="mui-card-content">
                <div class="mui-card-content-inner">
                  <p>商品货号：{{goodsInfo.goods_no}}</p>
                  <p>库存情况：{{goodsInfo.stock_quantity}}</p>
                  <p>上架时间：{{goodsInfo.add_time | timeF}}</p>
                </div>
              </div>
              <div class="mui-card-footer">
                <mt-button type="primary" size="large" @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
              </div>
            </div>

       
    </div>
</template>
<script>
import swiper from '@/components/swiper.vue'
import numbox from  '@/views/goods/numbox.vue'
import {getlunbo,getgoodinfo} from '@/api'
export default {   
    data(){
        return{
            id:this.$route.params.id,
            list:[],
            goodsInfo:{},
            ballFlag:false,
            selectedCount: 1
        }
    },
    created () {
        getlunbo({imgid:this.id}).then(res=>{
         
            if(res.status === 0){
                res.message.forEach(item=>{
                    item.img = item.src
                })

                this.list = res.message
            }

        });
        getgoodinfo({id:this.id}).then(res=>{
            // console.log(res,123);
            if(res.status ==0){
                this.goodsInfo = res.message[0]
            }
        })
    },
      components: {
        swiper,
        numbox
    },
    methods: {
        goComment(id){
            this.$router.push({name:'goodscom',params:{id}})
        },
        goDesc(id){
            this.$router.push({name:'goodsdec',params:{id}})
        },
         getSelectedCount(count){
            this.selectedCount = count;
        },
        addToShopCar(){
           
            this.ballFlag=!this.ballFlag;

            var goods = {
                id:this.id,
                count:this.selectedCount,
                price:this.goodsInfo.sell_price,
                selected:true
            };
            this.$store.commit('addtocar',goods);
           console.log(goods,1231);
        },
        //小球动画效果
        beforeEnter(el){
            el.style.transform = 'translate(0px,0px)'

        },
        enter(el,done){
            el.offsetWidth;
            //小球的位置
            const ballp = this.$refs.ball.getBoundingClientRect();
            //徽标的位置
            const biao = document.getElementById('badge').getBoundingClientRect();

            const x = biao.left-ballp.left
            const y = biao.top-ballp.top

            el.style.transform = `translate(${x}px,${y}px)`
            el.style.transition = 'all .5s cubic-bezier(.4,-0.3,1,.88)'
            done()
        },
        afterEnter(el) {
        this.ballFlag = !this.ballFlag;
        },
        }
}
</script>
<style lang="scss" scoped>
.goodinfo{
  background-color: #eee;
  overflow: hidden;
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  /* 小球 */
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 999;
    top: 390px;
    left: 146px;
  }}
</style>
